import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import pinia from './stores/index'
//配置跨域
import { jsonp } from 'vue-jsonp';
// 引入 echarts
import * as echarts from 'echarts'
import PanelHead from './components/panelHead.vue'

import 'st-common-ui-vue3/es/style.css'

import {useMenuStore} from "./stores/menuStore"


//elementplus相关-
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { it } from 'element-plus/es/locales.mjs'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

//使用pinia
app.use(pinia)

//刷新后的动态路由添加
const localDate =  localStorage.getItem('ak_gl')
const menuStore = useMenuStore() 
if(localDate){
  menuStore.dynamicMenu(JSON.parse(localDate).routerList)
  menuStore.routerList.forEach(item => {
    router.addRoute('main',item)
  });
}

//添加路由守卫
router.beforeEach((to,from)=>{
  const token = localStorage.getItem('ak_token')
  if(!token && to.path !== '/login'){
    return '/login'
  }else if(token && to.path=='/login'){
    return '/'
  }else{
    return true
  }
})

app.config.globalProperties.$jsonp= jsonp 
// 全局挂载 echarts
app.config.globalProperties.$echarts = echarts

app.component('PanelHead',PanelHead)
//使用路由
app.use(router)

app.mount('#app')
